package company.receipt;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class Util {
    public static void main1(String[] args) {

        OrderEntity orderEntity1 = new OrderEntity(1L,BigDecimal.valueOf(10),BigDecimal.valueOf(33080.48), LocalDateTime.parse("2022-06-18 14:38:30", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        OrderEntity orderEntity2 = new OrderEntity(2L,BigDecimal.valueOf(10.5),BigDecimal.valueOf(21769.5),LocalDateTime.parse("2022-06-18 13:28:43",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        OrderEntity orderEntity3 = new OrderEntity(3L,BigDecimal.valueOf(0),BigDecimal.valueOf(21291.1),LocalDateTime.parse("2022-06-18 16:54:51",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        OrderEntity orderEntity4 = new OrderEntity(4L,BigDecimal.valueOf(10),BigDecimal.valueOf(29410.76),LocalDateTime.parse("2022-06-19 14:15:43",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        OrderEntity orderEntity5 = new OrderEntity(5L,BigDecimal.valueOf(10),BigDecimal.valueOf(290000.76),LocalDateTime.parse("2022-06-18 19:15:43",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        List<OrderEntity> orderEntitys = List.of(orderEntity1, orderEntity2, orderEntity3, orderEntity4,orderEntity5);
        List<OrderEntity> collect = orderEntitys.stream().sorted(Comparator.comparing(OrderEntity::getWeightTime)).collect(Collectors.toList());
        ReceiptEntity receiptEntity = new ReceiptEntity(BigDecimal.valueOf(29400.76));
        List<Long> matchOrderIds = findMatchOrderIds(receiptEntity, collect);
        System.out.println(matchOrderIds);

    }



    private static List<Long> findMatchOrderIds(ReceiptEntity receiptEntity, List<OrderEntity> orderEntities) {
        // 返回订单id
        List<Long> resultOrderIds = new ArrayList<>();
        // 建立一个空队列
        Queue<OrderEntity> queue = new LinkedList();
        // 初始价格为0
        BigDecimal price = BigDecimal.ZERO;
        boolean next = true;
        for (int i = 0; i < orderEntities.size() || !queue.isEmpty(); i++) {


            if (price.compareTo(receiptEntity.getAmount()) == 0) {
                break;
            }
            if (price.compareTo(receiptEntity.getAmount()) == -1 && next ) {
                queue.offer(orderEntities.get(i));
                price = price.add(orderEntities.get(i).getTotalContract().subtract(orderEntities.get(i).getDebitAmount()));
                next = true;
                continue;
            }
            if (price.compareTo(receiptEntity.getAmount()) == 1) {
                OrderEntity poll = queue.poll();
                price = price.subtract(poll.getTotalContract().subtract(poll.getDebitAmount()));
                // 这里为什么要减一？是因为当前还不确定下一个是否可以入队。加size判断 是因为队列不为空的的时候要遍历完
                // 总数减完了比他小 继续入队
                if (price.compareTo(receiptEntity.getAmount()) == -1 && i < orderEntities.size()) {
                    i--;
                    next = true;
                } else {
                    next = false;
                }
                continue;
            }
            break;

        }
        if (price.compareTo(receiptEntity.getAmount()) == 0) {
            // 现在队列里为匹配上的订单ids
            while (!queue.isEmpty()) {
                resultOrderIds.add(queue.poll().getId());
            }
        }
        return resultOrderIds;
    }

    public static void main2(String[] args) throws IOException {
        File file = new File("test.jpg");
        file.createNewFile();
        // 生成指定url对应的二维码到文件，宽和高都是300像素
        QrCodeUtil.generate("https://hutool.cn/", 300, 300, FileUtil.file(file.getAbsolutePath()));
    }

    public static void main5(String[] args) {

        BigDecimal bigDecimal1 = BigDecimal.valueOf(0.21);
        BigDecimal bigDecimal2 = BigDecimal.valueOf(0.3);
        BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
        System.out.println((subtract.compareTo(BigDecimal.valueOf(0.1)) <= 0) && (subtract.compareTo(BigDecimal.valueOf(-0.1)) >= 0));

//        System.out.println((bigDecimal1.subtract(bigDecimal2).compareTo(BigDecimal.valueOf(0.1)) == 0)||(bigDecimal1.subtract(bigDecimal2).compareTo(BigDecimal.valueOf(-0.1)) == 0));


    }

    public static void main(String[] args) {

        List<Integer> path = new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> resource = List.of(1, 2, 3);
        back(resource, 2, 0, path, result);
        System.out.println(result);

    }

    private static void back(List<Integer> resource, int k, int startIndex, List<Integer> path, List<List<Integer>> result) {
        if (path.size() == k) {
            result.add(List.of(resource.get(path.get(0)), resource.get(path.get(1))));
            return;
        }
        for (int i = startIndex; i < resource.size(); i++) {
            path.add(i);
            back(resource, k, i + 1, path, result);
            path.remove(path.size() - 1);
        }
    }

}
